package com.hangwei.aicabinet;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.hangwei.aicabinet.model.bean.AllCabinetInfo;
import com.hangwei.aicabinet.net.ViewCallBack;
import com.hangwei.aicabinet.presenter.BindPresenter;
import com.hangwei.aicabinet.util.LoadingDialog;
import com.hangwei.aicabinet.util.SPKey;
import com.hangwei.aicabinet.util.SPUtil;
import com.hangwei.aicabinet.util.ToastUtil;
import com.hangwei.aicabinet.view.BindView;

import java.util.ArrayList;
import java.util.List;

/**
 * author: 张龙臣
 * date: 2018-08-28.
 */

public class BindActivity extends BaseActivity implements BindView {

    private BindPresenter mPresenter;
    private Spinner mSpinnerNum;
    private List<AllCabinetInfo.CabinetListBean> cabinetList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind);
        final View llView = findViewById(R.id.llView);
        Button btnBind = findViewById(R.id.btnBind);
//        EditText etBindNum = findViewById(R.id.etBindNum);
        mSpinnerNum = findViewById(R.id.spinnerNum);

        btnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TextView selectedView = (TextView) mSpinnerNum.getSelectedView();
                final String cabinetNum = selectedView.getText().toString().trim();
                if (TextUtils.isEmpty(cabinetNum)) {
                    ToastUtil.showCenterToast("请选择工具柜编号！");
                    return;
                }
                AllCabinetInfo.CabinetListBean cabinet= null;
                for (AllCabinetInfo.CabinetListBean bean : cabinetList) {
                    if (cabinetNum.equals(bean.cabinetNum)) {
                        cabinet = bean;
                    }
                }
                final Dialog dialog = LoadingDialog.showDialog(BindActivity.this, "绑定工具柜");
                assert cabinet != null;
                final AllCabinetInfo.CabinetListBean finalCabinet = cabinet;
                mPresenter.initCabinet(cabinet.cabinetId, new ViewCallBack() {
                    @Override
                    public void onNetworkStart() {
                        dialog.show();
                    }

                    @Override
                    public void onNetworkComplete(boolean isRight) {
                        if (dialog.isShowing()) dialog.dismiss();
                        if (isRight){
                            SPUtil.setString(SPKey.CABINET_NUM,cabinetNum);
                            SPUtil.setString(SPKey.CABINET_TYPE, finalCabinet.cabinetType);
                        }else SPUtil.setString(SPKey.CABINET_NUM,"");
                    }
                });
            }
        });

        mPresenter = new BindPresenter(this);
        final Dialog dialog = LoadingDialog.showDialog(this, "获取工具柜编号");
        mPresenter.getAllCabinetInfo(new ViewCallBack() {
            @Override
            public void onNetworkStart() {
                dialog.show();
            }

            @Override
            public void onNetworkComplete(boolean isRight) {
                if (dialog.isShowing()) dialog.dismiss();
            }
        });
    }

    @Override
    public void setCabinet(AllCabinetInfo data) {
        ArrayList<String> list = new ArrayList<>();
        cabinetList = data.cabinetList;
        for (AllCabinetInfo.CabinetListBean bean : cabinetList) {
            list.add(bean.cabinetNum);
        }
//        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, list);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.spinner_item, list);
        mSpinnerNum.setAdapter(adapter);
    }


    @Override
    public void bindSuccess(String id) {
        SPUtil.setString(SPKey.CABINET_ID, id);
//        if (Build.VERSION.SDK_INT >= 21)
//            startActivity(new Intent(BindActivity.this, LoginActivity.class),
//                    ActivityOptions.makeSceneTransitionAnimation(BindActivity.this).toBundle());
//        else
            startActivity(new Intent(BindActivity.this, LoginActivity.class));

        finish();
    }
}
